from sys import path as path2
from time import sleep, localtime, strftime
import base64
from threading import Thread, active_count

from bs4 import BeautifulSoup
from rich import print as rprint

path2.append('c:/users/tian/desktop')
from Service import service, ua
from service import title2tag, req


class Taosebt:
    def __init__(self, url, ipsl):
        service.path = 'd:/data/taosebt.db'
        self.URL = url
        self.newNum = 0     # 新增记录的数量
        self.R = req.Req(ipsl)

    def prepare(self, startIndex, endIndex):
        for i in range(startIndex, endIndex):
            indexUrl = f'{self.URL}/list.php?class=guochan&page={i}'
            while True:
                try:
                    node_a = self.R.req(indexUrl).find('ul', class_='list')('a')
                    break
                except Exception as e:
                    rprint('[red]索引页解析出错')
                    rprint('报错信息：', e)
                    sleep(3)
            for a in node_a:
                Thread(target=self.get, args=(a, i)).start()
                sleep(0.08)
            sleep(0.1)
        while True:         # 等待爬取线程都结束
            if active_count() == 1:
                break
            else:
                sleep(0.2)
        rprint(f'Taosebt 爬取完毕，本次新增 {self.newNum} 条数据')

    def get(self, a, i):
        title = base64.b64decode(a.script.string.split("'")[1])     # 二进制串
        title = ''.join(title.decode('utf-8').split()).lower()      # 删除空格或多个连续空格
        tag = title2tag.aboutTitle(service.path, title, i)
        if not tag:
            return
        url = self.URL + a.attrs['href']
        _div = self.R.req(url).find('div', class_='p-3')
        postDate = _div.div.p.string.split('：')[-1]
        size = _div.div('p')[2].string.split('：')[-1]
        duration = _div.div('p')[3].string.split('：')[-1]
        _magnet = _div('div', class_='container')[-1].div('div')[1].a.attrs['href']
        magnet = _magnet.split("&")[0].lower()                 # 去掉 & 后面的参数，并将所有字母小写
        recordDate = strftime('%Y-%m-%d %H:%M', localtime())
        status = 0
        service.exec('insert into guochan (tag, title, url, magnet, postdate, size, duration, recorddate, status) values (?, ? ,? , ?, ?, ?, ?, ?, ?)',
                    (tag, title, url, magnet, postDate, size, duration, recordDate, status))
        self.newNum += 1
        rprint(f'{self.newNum} ({i})')



    

